package org.jeecg.modules.ws.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.ws.entity.WsBankInfo;
import org.jeecg.modules.ws.entity.WsBankForewarn;
import org.jeecg.modules.ws.service.IWsBankInfoService;
import org.jeecg.modules.ws.service.IWsBankForewarnService;
import org.jeecg.modules.ws.util.WyzlBalanceRequest;
import org.jeecg.modules.ws.util.WyzlBalanceResponse;
import org.jeecg.modules.ws.util.WyzlClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 银行余额查询定时任务
 */
@Slf4j
@Component
public class BankBalanceJob {

    @Autowired
    private IWsBankInfoService wsBankInfoService;

    @Autowired
    private IWsBankForewarnService forewarnService;

    @Autowired
    private WyzlClient wyzlClient;

    /**
     * 每小时执行一次
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void queryBalance() {
        log.info("开始执行银行余额查询定时任务");
        try {
            // 查询所有银行账户信息
            List<WsBankInfo> bankInfoList = wsBankInfoService.list();

            for (WsBankInfo bankInfo : bankInfoList) {
                try {
                    // 构建余额查询请求
                    WyzlBalanceRequest request = new WyzlBalanceRequest();

                    // 设置交易信息
                    WyzlBalanceRequest.TxInfo txInfo = new WyzlBalanceRequest.TxInfo();
                    txInfo.setAccNo(bankInfo.getBankNo());
                    txInfo.setCurrCod("156"); // 人民币
                    request.setTxInfo(txInfo);

                    // 发送请求
                    WyzlBalanceResponse response = wyzlClient.queryBalance(request);

                    // 更新余额信息
                    if ("000000".equals(response.getReturnCode())) {
                        BigDecimal newBalance = new BigDecimal(response.getTxInfo().getBalance());
                        bankInfo.setBalance(newBalance);
                        wsBankInfoService.updateById(bankInfo);
                        log.info("账户[{}]余额更新成功，余额：{}", bankInfo.getBankNo(), response.getTxInfo().getBalance());

                        // 检查预警状态
                        WsBankForewarn forewarn = forewarnService.getByBankNo(bankInfo.getBankNo());
                        if (forewarn != null && "1".equals(forewarn.getForewarnNoticeStatus())) {
                            // 如果余额高于预警值，重置预警通知状态
                            if (newBalance.compareTo(forewarn.getForewarnValue()) >= 0) {
                                forewarn.setForewarnNoticeStatus("0");
                                forewarnService.updateById(forewarn);
                                log.info("账户[{}]余额已恢复至预警值以上，重置预警通知状态", bankInfo.getBankNo());
                            }
                        }
                    } else {
                        log.error("账户[{}]余额查询失败，错误码：{}，错误信息：{}",
                            bankInfo.getBankNo(), response.getReturnCode(), response.getReturnMsg());
                    }
                } catch (Exception e) {
                    log.error("账户[{}]余额查询异常", bankInfo.getBankNo(), e);
                }
            }
        } catch (Exception e) {
            log.error("银行余额查询定时任务执行异常", e);
        }
        log.info("银行余额查询定时任务执行完成");
    }
}
